﻿using System;
using System.Linq.Expressions;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using JustinAngel.Neurons.Framework.Helpers;
using JustinAngel.Neurons.Framework.Messages;
using JustinAngel.Neurons.Framework.Services;
using JustinAngel.Neurons.ServerModel;
using JustinAngel.Neurons.Services;
using Microsoft.Phone.Shell;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives;

namespace JustinAngel.Neurons.Framework
{
    public class VViewModelBase : ViewModelBase
    {
        public VViewModelBase()
        {
            MessengerInstance = IoC.Get<IMessenger>();
            DataService = IoC.Get<IDataService>();
        }

        private string _state;
        public string CurrentStateName
        {
            get { return _state; }
            set
            {
                _state = value;
                this.RaisePropertyChanged(() => CurrentStateName);
            }
        }

        protected void RaisePropertyChanged<T>(Expression<Func<T>> property)
        {
            RaisePropertyChanged(property.GetMemberName());
        }

        protected void RaisePropertyChangedAndUpdateVSM<T>(Expression<Func<T>> property, bool value)
        {
            var propName = property.GetMemberName();
            RaisePropertyChanged(propName);

            if (value)
            {
                CurrentStateName = propName;
            }
            else
            {
                CurrentStateName = "Not" + propName;
            }
        }

        public bool IsInEmulator
        {
            get
            {
                if (!IsInDesignMode)
                {
                    return IoC.Get<IDeviceHelper>().GetIsInEmulator();
                }
                else
                {
                    return false;
                }
            }
        }

        private object _data;
        public object Data
        {
            get { return _data; }
            set
            {
                _data = value;
                OnDataSet();
            }
        }

        public virtual void OnDataSet() { }

        public void BeginInvokeOnUIThread(Action action)
        {
            var Dispatcher = Deployment.Current.Dispatcher;
            if (Dispatcher.CheckAccess() == false)
            {
                Deployment.Current.Dispatcher.BeginInvoke(action);
            }
            else
            {
                action();
            }
        }

        [Dependency]
        public IDataService DataService { get; set; }

        public void WithRssRootItem(Action<RssRootItem> action)
        {
            DataService.WithRootRssItem(action);
        }

        //protected void WriteStateForProperty(Expression property, T value)
        //{
        //    PhoneApplicationService.Current.State[this.GetType().Name + "_" + property.GetMemberName()] = value;
        //}

        //protected T2 ReadStateForProperty<T, T2>(Expression<Func<T, T2>> property)
        //{
        //   return (T2) PhoneApplicationService.Current.State[this.GetType().Name + "_" + property.GetMemberName()];
        //}
    }
}
